﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

using System.Drawing;

namespace Qreed.Windows
{
    internal class ScopeControl : UserControl
    {
        private const int _topButtonPadding = 3;
        private const int _leftRightPadding = 4;

        private Button closeButton;
        private ToolTip toolTip;
        
        private System.ComponentModel.IContainer components;

        public ScopeControl(TextScope scope) : base()
        {
            Scope = scope;

            SuspendLayout();

            components = new Container();

            InitializeButton();
            InitializeToolTip();

            Text = scope.Name;

            ResumeLayout(false);
        }

        private void InitializeToolTip()
        {
            toolTip = new ToolTip(this.components);
            toolTip.IsBalloon = true;
        }

        private void InitializeButton()
        {
            closeButton = new Button();

            this.closeButton.BackColor = System.Drawing.SystemColors.Window;
            this.closeButton.Cursor = System.Windows.Forms.Cursors.Hand;
            this.closeButton.FlatAppearance.BorderColor = System.Drawing.SystemColors.Window;
            this.closeButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.closeButton.Image = Resources.DeleteScopeImage;
            this.closeButton.Name = "closeButton";
            this.closeButton.Size = new System.Drawing.Size(11, 11);
            this.closeButton.TabIndex = 0;
            this.closeButton.TabStop = false;
            this.closeButton.UseVisualStyleBackColor = false;
            this.closeButton.Click += new EventHandler(OnCloseButtonClick);

            this.Controls.Add(this.closeButton);
        }

        public event EventHandler RemoveScope;

        void OnCloseButtonClick(object sender, EventArgs e)
        {
            if (RemoveScope != null)
                RemoveScope(this, null);    
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            Size sizeText = TextRenderer.MeasureText(Text, Font);
            Width = sizeText.Width + 4 + closeButton.Width;
            closeButton.Location = new Point(Width - closeButton.Width - 4, 4);

            toolTip.SetToolTip(closeButton, "Remove \"" + Text + "\"");
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.Clear(Parent.BackColor);

            Rectangle rect = new Rectangle(1, 1, Width - 3, Height - 3);
            e.Graphics.DrawRectangle(Pens.LightGray, rect);
            
            TextRenderer.DrawText(e.Graphics, Text, Font, e.ClipRectangle, Color.Black, TextFormatFlags.VerticalCenter);
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(75, Font.Height + _topButtonPadding * 2);
            }
        }
        
        private TextScope _scope;
        public TextScope Scope
        {
            get { return this._scope; }
            set { this._scope = value; }
        }
    }
}
